package com.example.hxds.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.example.hxds.bff.driver.controller.form.*;
import com.example.hxds.bff.driver.service.DriverService;
import com.example.hxds.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机模块Web接口")
public class DriverController {
    @Resource
    private DriverService driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R registerNewDriver(@RequestBody @Valid RegisterNewDriverForm form){
        long driverId=driverService.registerNewDriver(form);
        StpUtil.login(driverId);
        String token=StpUtil.getTokenInfo().getTokenValue();
        return R.ok().put("token",token);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新实名认证信息")
    @SaCheckLogin // 鉴权
    public R updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form){
        long driverId=StpUtil.getLoginIdAsLong(); // 从token中解析得到用户id，即driverId
        form.setDriverId(driverId);
        int rows=driverService.updateDriverAuth(form);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建司机人脸模型归档")
    @SaCheckLogin // 鉴权
    public R createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelForm form){
        long driverId=StpUtil.getLoginIdAsLong(); // 从token中解析得到用户id，即driverId
        form.setDriverId(driverId);
        String result=driverService.createDriverFaceModel(form);
        return R.ok().put("result",result);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录")
    public R login(@RequestBody @Valid LoginForm form){
        HashMap map = driverService.login(form);
        if (map != null) // 拿到司机注册信息
        {
            long driverId=MapUtil.getLong(map,"id");// 从返回的登录信息中拿到driverId
            byte realAuth = Byte.parseByte(MapUtil.getStr(map, "realAuth")); // 司机是否认证
            Boolean archive = MapUtil.getBool(map, "archive"); // 司机状态
            StpUtil.login(driverId); // 把driverId塞到login方法里面
            String token = StpUtil.getTokenInfo().getTokenValue();// 根据driverId生成令牌
            return R.ok().put("token",token).put("realAuth",realAuth).put("archive",archive);
        }
        return R.ok();
    }

    @PostMapping("/searchDriverBaseInfo")
    @Operation(summary = "查询司机基本信息")
    public R searchDriverBaseInfo(){
        long driverId=StpUtil.getLoginIdAsLong(); // 从token中解析得到用户id，即driverId
        SearchDriverBaseInfoForm form = new SearchDriverBaseInfoForm();
        form.setDriverId(driverId);
        HashMap map = driverService.searchDriverBaseInfo(form);
        return R.ok().put("result",map);
    }

    @GetMapping("/logout")
    @Operation(summary = "司机退出系统")
    public R logout(){
        StpUtil.logout();
        return R.ok();
    }

    @PostMapping("/searchWorkbenchData")
    @Operation(summary = "查询司机工作台数据")
    @SaCheckLogin // 检查是否登录
    public R searchWorkbenchData()
    {
        long driverId = StpUtil.getLoginIdAsLong(); // 从token中解析得到用户id，即driverId
        HashMap map = driverService.searchWorkbenchData(driverId);
        return R.ok().put("result",map);
    }

    @GetMapping("/searchDriverAuth")
    @Operation(summary = "查询司机认证信息")
    @SaCheckLogin
    public R searchDriverAuth()
    {
        long driverId = StpUtil.getLoginIdAsLong(); // 从token中解析得到用户id，即driverId
        SearchDriverAuthForm form = new SearchDriverAuthForm();
        form.setDriverId(driverId);
        HashMap map = driverService.searchDriverAuth(form);
        return R.ok().put("result",map);
    }
}
